package com.zzyy.study.stack;

/**
 * Description:
 * <p>
 * ClassName: Calculator
 * date: 2021/2/24 11:02
 *
 * @author zzjx
 * @version 1.0
 * @since JDK 1.8
 */
public class Calculator {
    public static void main(String[] args) {
         //根据前面思路完成表达式运算
        String expression = "70+2*6-2";
        ArrayStack2 numStack = new ArrayStack2(10);
        ArrayStack2 operStack = new ArrayStack2(10);
        //定义相关变量
        int index =0;
        int num1 = 0;
        int num2 =0;
        int oper = 0;
        int res = 0;
        String keepNum = "";//用于拼接多位数
        char ch =' ';//将每次扫描得到的char保存到ch总
        //开始while循环扫描expression
        while (true){
            //依次得到expression每一个字符
            ch = expression.substring(index,index+1).charAt(0);
            //判断ch是什么然后做相应的处理
            if(operStack.isOper(ch)){//如果是运算符
                //判断当前的符号栈是否为空 如果当前的操作符的优先级小于或者等于栈中的操作符，就需要从数栈pop出来俩个数
                if(!operStack.isEmpty()){
                    if(operStack.priority(ch) <= operStack.priority(operStack.peek())){
                        num1 =numStack.pop();
                        num2 =numStack.pop();
                        oper = operStack.pop();
                        res= numStack.cal(num1,num2,oper);
                        //把运算的结果入数栈
                        numStack.push(res);
                        //然后将当前操作符入符号栈
                        operStack.push(ch);
                    }else {
                        operStack.push(ch);

                    }
                }else {
                    //如果为空直接入栈
                    operStack.push(ch);
                }
            }else {//如果是数字直接入数字栈
                //numStack.push(ch-48);

                keepNum += ch;
                //判断下一个字符是不是数字，如果是数字，就继续扫描，如果是运算符，则入栈
                //注意是看后一位，不是index++
                //如果ch已经是expression的最后一位，就直接入栈
                if(index == expression.length()-1){
                    numStack.push(Integer.parseInt(keepNum));
                }else{
                    if(operStack.isOper(expression.substring(index+1,index+2).charAt(0))){
                        //如果后一位是运算符,则入栈
                        numStack.push(Integer.parseInt(keepNum));
                        keepNum = "";
                    }
                }
            }
            //让index +1,并判断是否扫描到expression最后
            index++;
            if(index>=expression.length()){
                break;
            }
        }
        //表达式扫描完毕
        while (true){
            //如果符号栈为空则为计算到最后的结果，数栈中只有一个数字
            if(operStack.isEmpty()){
                break;
            }
            num1 =numStack.pop();
            num2 =numStack.pop();
            oper = operStack.pop();
            res= numStack.cal(num1,num2,oper);
            numStack.push(res);
        }
        System.out.println(numStack.pop());
    }
}
//创建一个栈
class ArrayStack2 {
    private int maxSize;
    private int[] stack;
    private int top = -1; //top表示栈顶，初始化为-1

    //构造器
    public ArrayStack2(int maxSize) {
        this.maxSize = maxSize;
        stack = new int[maxSize];
    }

    //栈满
    public boolean isFull() {
        return top == maxSize - 1;
    }

    //栈空
    public boolean isEmpty() {
        return top == -1;
    }

    //入栈
    public void push(int value) {
        //先判断栈是否满
        if (isFull()) {
            System.out.println("栈满");
            return;
        }
        top++;
        stack[top] = value;
    }

    //出栈 ，将栈顶的数据返回
    public int pop() {
        //先判断栈是否空
        if (isEmpty()) {
            System.out.println();
            //抛出异常
            throw new RuntimeException("异常");
        }
        int value = stack[top];
        System.out.println(value);
        top--;
        return value;
    }
    //显示栈的情况[遍历栈]
    public void list() {
        if (isEmpty()) {
            System.out.println("栈空，没有数据~~");
            return;
        }
        for (int i = top; i >= 0; i--) {
            System.out.printf("stack[%d]=%d\n", i, stack[i]);
        }
    }
    //返回运算符的优先级，优先级是程序员来确定的,优先级使用数字表示
    //数字越大，则优先级越高
    public int priority(int oper){
        if(oper == '*' || oper == '/'){
             return 1;
        }else if (oper == '+' || oper == '-') {
            return 0;
        }else {
            return -1; //假定目前的表达式只有+ , - , * ,/
        }
    }
    //判断是不是一个运算符
    public boolean isOper(char val){
        return val == '+' || val == '-' || val == '*' || val== '/';
    }
    //计算方法
    public int cal (int num1 ,int num2 ,int oper){
           int res = 0; //res用于存放计算的结果
        switch (oper){
            case '+':
                res = num1 +num2;
                break;
            case '-':
                res = num2 - num1;
                break;
            case  '*':
                res = num1 * num2;
                break;
            case '/':
                res = num2 /num1;
                break;
            default:
                break;
        }
        return res;
    }
    //增加一个方法可以返回栈顶的值，但是不是真正pop
    public int peek(){
        return stack[top];
    }
}
